home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / backgrnd.zip / BACKGRND.C next >
Text File  |  1989-09-18  |  3KB  |  137 lines

  1. /*
  2.  * @(#) BACKGRND.C  V1.0           Author: Shawn M. Regan
  3.  *
  4.  * Turbo C version 2.0
  5.  *
  6.  * This was an attempt at multitasking. The program first loads the
  7.  * address of it's controlling routine at interrupt vector x1C then
  8.  * terminates and stays resident via interrupt x27. Interrupt 1C is
  9.  * called at every clock tick which essentially equally divides it's
  10.  * time by two. Video display is done by writing to direct video ram
  11.  * as not to collide with dos or bios calls.
  12.  *
  13.  */
  14.  
  15. #include <dos.h>
  16. #include <stdlib.h>
  17.  
  18. void interrupt tsr_ap();
  19. void tsr(),write_char(),set_vid_mem();
  20.  
  21. char far * vid_mem;
  22.  
  23. main()
  24. {
  25.   struct address {
  26.     char far *p;
  27.     };
  28.  
  29. /* Compute interrupt's address, store in addr */
  30. struct address far *addr = ( struct address far *) 112;
  31.  
  32. clrscr();
  33. printf("Installed...BACKGNRD by Shawn Regan, Atlanta, GA.\n\n");
  34. printf("This program is not copyrighted and hereby declared as public domain.\n");
  35. printf("It provides a good example of using interrupt calls 27 & 1C. If you\n");
  36. printf("are interested in this you should pick up these two books which were\n");
  37. printf("instrumental in writing this program:\n\n");
  38. printf("The C Power User's Guide - By Herbert Schildt\n");
  39. printf("The Peter Norton Programmer's guide to the IBM PC\n\n");
  40. printf("By the way if you use and enjoy this program please take $10 and stick\n");
  41. printf("it in your pocket. I have a real job and don't need your money. Better\n");
  42. printf("yet go and buy Herb Schildt's book which contains a wealth of advanced\n");
  43. printf("programming information and techniques.\n");
  44. sleep(5);
  45. addr->p = (char far *) tsr_ap;
  46. set_vid_mem();
  47. tsr(1200);
  48. }
  49.  
  50. void interrupt tsr_ap()
  51. {
  52. static char alpha[] = { "ShawnRegan" };
  53. static int j=0,i=0;
  54.  
  55. i=j/10;
  56. write_char(alpha[i]);
  57. if (++j > 99 ) j=0;     /* Each character get's 10 turns in a row */
  58. }
  59.  
  60. /* Set video mode - directly from the C power user's guide */
  61. void set_vid_mem()
  62. {
  63. int vmode;
  64.  
  65. vmode=video_mode();
  66. if ((vmode != 2 ) && (vmode != 3 ) && (vmode != 7 )) {
  67.   printf("video must be in 80 column text mode");
  68.   exit(1);
  69.   }
  70. if ( vmode == 7 ) vid_mem = (char far *) 0xB0000000;
  71. else vid_mem = ( char far *) 0xB8000000;
  72. }
  73.  
  74. /* Load program and leave in memory - from C power user's guide also */
  75. void tsr(size)
  76. unsigned size;
  77. {
  78.   union REGS r;
  79.  
  80.   r.h.ah = 49;      /* Terminate but stay resident */
  81.   r.h.al = 0;     /* Return code */
  82.   r.x.dx = size;
  83.   int86(0x21,&r,&r);
  84. }
  85.  
  86.  
  87. /* Compute new character's position then send the character directly to
  88.  * video ram.
  89.  */
  90. void write_char(ch)
  91. char ch;
  92. {
  93. register int a,b;
  94. static int x=0,dx=1,y=24,dy=1;
  95. char far *v;
  96.  
  97. v=vid_mem+(x*160)+y*2;    /* Clear previous character */
  98. *v=' ';
  99.  
  100. a=x+dx;
  101. b=y+dy;
  102. v=vid_mem+(a*160)+b*2;    /* If occupied compute random new direction */
  103. if ( *v != ' ' || a > 24 || a < 0 || b > 79 || b < 0 ) {
  104.   do  {
  105.   dx=rnd();
  106.   dy=rnd();
  107.   } while(dx==0 && dy==0);    /* Both axis cannot move zero units */
  108.   }
  109. else {      /* not occupied continue on current course */
  110.   x=a;
  111.   y=b;
  112.   }
  113. v=vid_mem+(x*160)+y*2;    /* Write new character */
  114. *v=ch;
  115. }
  116.  
  117. /* Get current video mode, C power user's guide */
  118. int video_mode()
  119. {
  120. union REGS r;
  121.  
  122. r.h.ah = 15;
  123. return int86(0x10, &r, &r) & 255;
  124. }
  125.  
  126.  
  127. /* Return random, -1, 0 or 1 */
  128. int rnd()
  129. {
  130. int i;
  131.  
  132. i=rand();
  133. if ( i < 10000) return -1;
  134. if ( i < 20000) return 0;
  135. return 1;
  136. }
  137.